/*
===========================================================================
Copyright (C) 2013-2014 Avotu Briezhaudzetava

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

===========================================================================
*/

//
// includes
//
#include "precursormodel.h"

/*
================
data
================
*/
QVariant PrecursorModel::data( const QModelIndex &index, int role ) const {
    if ( !index.isValid())
        return QVariant();

    if ( index.row() >= m.precursorList.count())
        return QVariant();

    if ( role == Qt::DisplayRole ) {
        PrecursorEntry *pPtr;
        QString base;

        // failsafe
        pPtr = m.precursorList.at( index.row());
        if ( pPtr == NULL )
            return QVariant();

        // gen base string
        base = QString( "%1 (%2 %3" )
                .arg( pPtr->title())
                .arg( QString::number( pPtr->baseAmount(), 'f', Main::getPrecision( pPtr->units())))
                .arg( Common::UnitsStrings[pPtr->units()] );

        if ( m.precursorList.at( index.row())->isSolvent()) {
            Common::Units secondaryUnits;

            if ( m.precursorList.at( index.row())->units() == Common::Millilitres )
                secondaryUnits = Common::Grams;
            else
                secondaryUnits = Common::Kilograms;

            return QString( "%1 / %2 %3)" )
                    .arg( base )
                    .arg( QString::number( pPtr->baseAmountTo( secondaryUnits ), 'f', Main::getPrecision( secondaryUnits )))
                    .arg( Common::UnitsStrings[secondaryUnits] );
        } else {
            return base.append( ")" );
        }
    }
    else if ( role == Qt::UserRole )
        return m.precursorList.at( index.row())->id();
    else
        return QVariant();
}

/*
================
flags
================
*/
Qt::ItemFlags PrecursorModel::flags( const QModelIndex &index ) const {
    if ( !index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags( index );
}
